package com.jthinker.blog.ext.fastjson.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.jthinker.blog.ext.fastjson.filter.LazyPropertyFilter;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.OutputStream;

/**
 * User: Gang Chen
 * Date: 2015/8/11 20:46
 */
public class CusFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter
{
    private final LazyPropertyFilter filter = new LazyPropertyFilter();

    @Override
    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
    {
        OutputStream out = outputMessage.getBody();
        String text = JSON.toJSONString(obj, filter, getFeatures());
        byte[] bytes = text.getBytes(getCharset());
        out.write(bytes);
    }
}
